{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "张三在食堂吃饭\n",
      "张三在学习\n",
      "准备调用父方法\n",
      "张三在吃饭\n",
      "李四在吃饭\n",
      "A eat\n",
      "B eat\n",
      "C eat\n",
      "A eat\n",
      "(<class '__main__.C'>, <class '__main__.A'>, <class '__main__.B'>, <class 'object'>)\n"
     ]
    }
   ],
   "source": [
    "#继承  继承是面向对象编程的一个重要的方式，因为通过继承，子类就可以扩展父类的功能。\n",
    "class Person:\n",
    "    age=0\n",
    "    name=\"\"    \n",
    "    def __init__(self,name,age):\n",
    "        self.name=name\n",
    "        self.age=age\n",
    "    def eat(self):\n",
    "        print(\"{}在吃饭\".format(self.name))\n",
    "class Student(Person):#继承父类\n",
    "    def study(self): #子类方法 增加自己的功能\n",
    "        print(\"{}在学习\".format(self.name))#调用父类方法\n",
    "    def eat(self):#重写父类方法，在子类中定义与父类相同名称的方法\n",
    "        print(\"{}在食堂吃饭\".format(self.name))\n",
    "    def eat2(self):\n",
    "        print(\"准备调用父方法\")\n",
    "        super().eat()#调用父类方法\n",
    "class Teacher(Person):\n",
    "    pass # 占位符 代码中类下面不写任何东西，会自动跳过，不报错\n",
    "s=Student(\"张三\",18)\n",
    "s.eat()#调用子类方法\n",
    "s.study()#调用父类方法\n",
    "s.eat2() #扩展方法，通过supper调用父方法的eat方法\n",
    "t=Teacher(\"李四\",20)\n",
    "t.eat()#调用父类方法\n",
    "#多继承\n",
    "class A:\n",
    "    def eata(self):\n",
    "        print(\"A eat\")\n",
    "    def eat(self):\n",
    "        print(\"A eat\")  \n",
    "class B:\n",
    "    def eatb(self):\n",
    "        print(\"B eat\")\n",
    "    def eat(self):\n",
    "        print(\"B eat\")  \n",
    "class C(A,B): #不同的父类，最好不要有同名的方法\n",
    "    def eatc(self):\n",
    "        print(\"C eat\")\n",
    "c=C()\n",
    "c.eata()#调用A类方法\n",
    "c.eatb()#调用B类方法\n",
    "c.eatc()#调用C类方法\n",
    "c.eat()#调用A类方法  多继承时，如果父类有同名的方法，会调用最左边的父类的方法\n",
    "print(C.__mro__)#查看继承顺序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
